#ifndef _IMAGE_H_
#define _IMAGE_H_

#include <assert.h>
#include "vectors.h"

// ====================================================================
// ====================================================================
// Simple image class

class Image {

public:

	// ========================
	// CONSTRUCTOR & DESTRUCTOR
	Image(int w, int h) {
		width = w;
		height = h;
		data = new Vec3f[width*height]; }
	~Image() {
		delete [] data; }

	// =========
	// ACCESSORS
	int Width() const { return width; }
	int Height() const { return height; }
	const Vec3f& GetPixel(int x, int y) const {
		assert(x >= 0 && x < width);
		assert(y >= 0 && y < height);
		return data[y*width + x]; }
	const Vec3f GetLinearPixel(float x, float y){		//[0,1)
		if (x<0.f) x += int(-x)+1; if (y<0.f) y += int(-y)+1;
		x -= int(x); y -= int(y);
		x *= width; y *= height; //x -= 0.5f; y -= 0.5f;
		int i,j; float a,b;
		i = floor(x); j = floor(y); a = x - i; b = y - j;
		int i1,j1; i1 = (i+1)==width? 0:(i+1); j1 = (j+1)==height? 0:(j+1);
		return GetPixel(i,j)*(1-a)*(1-b) + GetPixel(i1,j)*a*(1-b) 
			+ GetPixel(i,j1)*b*(1-a) + GetPixel(i1,j1)*a*b;
	}

	// =========
	// MODIFIERS
	void SetAllPixels(const Vec3f &color) {
		for (int i = 0; i < width*height; i++) {
			data[i] = color; } }
	void SetPixel(int x, int y, const Vec3f &color) {
		assert(x >= 0 && x < width);
		assert(y >= 0 && y < height);
		data[y*width + x] = color; }

	// ===========
	// LOAD & SAVE
	static Image* LoadPPM(const char *filename);
	void SavePPM(const char *filename) const; 
	static Image* LoadTGA(const char *filename);
	void SaveTGA(const char *filename) const; 

	// extension for image comparison
	static Image* Compare(Image* img1, Image* img2);

private:

	// ==============
	// REPRESENTATION
	int width;
	int height;
	Vec3f *data;

};

// ====================================================================
// ====================================================================

#endif

